# coding:utf-8

from wtforms import StringField, HiddenField, TextAreaField
from wtforms_components import IntegerField, DateField
from wtforms.validators import DataRequired, Optional

from app.op.constant import AreaType
from app.enterprise.models import HpProductType, HpProjectType, HpSecuritySystem
from ..forms import (
    WphForm, MultiCheckboxField, NonValidatingSelectField, NotChoices
)
from common.constant import ColorRule, SpanRule, SizeOperator


class EnterpriseForm(WphForm):
    """
    create by ld_wu 2018-05-09
    """
    enterprise_name = StringField('企业名称', validators=[DataRequired()])
    enterprise_address = StringField('企业地址', validators=[Optional()])
    level = HiddenField(default=99)
    # dispatch_cnt = IntegerField()
    user_id = HiddenField()
    start_date = DateField('开通日期', validators=[DataRequired()])
    expired_date = DateField('结束日期', validators=[DataRequired()])
    dispatch_limit = IntegerField('派单上限', validators=[DataRequired()])
    mobile = StringField('手机号')
    name = StringField('姓名')
    sub_account = HiddenField()
    area_ids = MultiCheckboxField('关注地区', validators=[Optional()], coerce=int)
    account_limit = IntegerField('子账号数量', default=0)
    remark = StringField('备注', validators=[Optional()])
    begin_time = DateField(
        '开始时间',
        validators=[Optional()]
    )
    end_time = DateField(
        '结束时间',
        validators=[Optional()]
    )

    def __init__(self, *args, **kwargs):
        super(EnterpriseForm, self).__init__(*args, **kwargs)

        from app.op.models import Area
        cities = Area.query.filter_by(
            type=AreaType.CITY.value
        ).order_by(Area.id.asc())

        self.area_ids.choices = [(i.id, i.name) for i in cities]


class EnterpriseSearchForm(WphForm):
    enterprise_name = StringField('企业名称')
    mobile = StringField('手机号')
    prov = NonValidatingSelectField(
        '省份', validators=[Optional()], coerce=int
    )

    def __init__(self, *args, **kwargs):
        super(EnterpriseSearchForm, self).__init__(*args, **kwargs)

        from app.op.models import Area
        provs = Area.query.filter_by(
            type=AreaType.PROV.value
        ).order_by(Area.id.asc())

        self.prov.choices = NotChoices + [
            (area.id, area.name)
            for area in provs
        ]


class ProjectBaseTypeForm(WphForm):
    name = StringField('项目类型', validators=[DataRequired()])


class SecurityBaseTypeForm(WphForm):
    name = StringField('保障体系类型', validators=[DataRequired()])


class ProductBaseTypeForm(WphForm):
    name = StringField('产品类型', validators=[DataRequired()])


class EnterpriseHomepageForm(WphForm):
    """
    create by lina
    """
    title = StringField('标题', validators=[Optional()])
    description = TextAreaField('描述', validators=[Optional()])
    keywords = TextAreaField('关键字', validators=[Optional()])
    hostname = StringField('域名', validators=[Optional()])

    base_color_num = StringField('主色号', validators=[Optional()])
    first_screen_color_num = StringField('首屏色号', validators=[Optional()])
    project_color_num = StringField('项目案例色号', validators=[Optional()])
    security_color_num = StringField('保障体系色号', validators=[Optional()])
    product_color_num = StringField('产品展示色号', validators=[Optional()])
    partner_color_num = StringField('合作伙伴色号', validators=[Optional()])
    about_us_color_num = StringField('了解我们色号', validators=[Optional()])
    scale_color_num = StringField('公司规模色号', validators=[Optional()])

    header_logo = StringField('页眉logo', validators=[Optional()])
    footer_logo = StringField('页脚logo', validators=[Optional()])

    first_screen_background = StringField('首屏背景图', validators=[Optional()])
    first_screen_title = TextAreaField('首屏标题', validators=[Optional()])
    first_screen_subtitle = TextAreaField('首屏副标题', validators=[Optional()])

    virtual_number = StringField('虚拟号', validators=[Optional()])
    virtual_number_img = StringField('虚拟号图片', validators=[Optional()])

    project_subtitle = TextAreaField('项目案例副标', validators=[Optional()])

    product_subtitle = TextAreaField('产品展示副标', validators=[Optional()])

    security_subtitle = TextAreaField('保障体系副标', validators=[Optional()])

    partner_subtitle = TextAreaField('合作伙伴副标', validators=[Optional()])
    partner_img = StringField('合作伙伴图片', validators=[Optional()])

    about_us_subtitle = TextAreaField('了解我们副标', validators=[Optional()])
    legal_person_avatar = StringField('法人头像', validators=[Optional()])
    legal_person_name = StringField('法人姓名', validators=[Optional()])
    registered_capital = StringField('注册资金', validators=[Optional()])
    registered_date = StringField('注册时间', validators=[Optional()])
    staff_size = StringField('人员规模', validators=[Optional()])

    safety_record = TextAreaField('安备', validators=[Optional()])
    record_number = TextAreaField('备案号', validators=[Optional()])

    agent_name = StringField('客服名称', validators=[Optional()])
    agent_avatar = StringField('客服头像', validators=[Optional()])

    def __init__(self, *args, **kwargs):
        super(EnterpriseHomepageForm, self).__init__(*args, **kwargs)


class EnterpriseProjectForm(WphForm):
    weight = StringField('排序', validators=[Optional()])
    project_type_id = NonValidatingSelectField(
        '项目类型', validators=[Optional()], coerce=int
    )
    project_cost = StringField('项目造价', validators=[Optional()])
    project_title = StringField('项目标题', validators=[Optional()])
    project_text = StringField('项目内容', validators=[Optional()])
    project_img = StringField('项目图片', validators=[Optional()])

    def __init__(self, *args, **kwargs):
        super(EnterpriseProjectForm, self).__init__(*args, **kwargs)
        project_types = HpProjectType.query.order_by(
            HpProjectType.id.asc()
        ).all()
        self.project_type_id.choices = NotChoices + [
            (i.id, i.name)
            for i in project_types
        ]


class EnterpriseProductForm(WphForm):
    """
    add by zhouhl 2018-05-24
    企业产品展示表单
    """
    weight = StringField('排序', validators=[Optional()])
    product_type_id = NonValidatingSelectField(
        '产品类型', validators=[Optional()], coerce=int
    )
    product_img = StringField('产品图片', validators=[Optional()])
    product_title = StringField('产品标题', validators=[Optional()])

    def __init__(self, *args, **kwargs):
        super(EnterpriseProductForm, self).__init__(*args, **kwargs)
        product_types = HpProductType.query.order_by(
            HpProductType.id.asc()
        ).all()
        self.product_type_id.choices = NotChoices + [
            (i.id, i.name)
            for i in product_types
        ]


class EnterpriseSecurityForm(WphForm):
    """
    add by zhouhl 2018-05-24
    企业保障体系表单
    """
    weight = StringField('排序', validators=[Optional()])
    security_id = NonValidatingSelectField(
        '保障体系', validators=[Optional()], coerce=int
    )
    security_text = StringField('保障说明', validators=[Optional()])

    def __init__(self, *args, **kwargs):
        super(EnterpriseSecurityForm, self).__init__(*args, **kwargs)
        security_systems = HpSecuritySystem.query.order_by(
            HpSecuritySystem.id.asc()
        ).all()
        self.security_id.choices = NotChoices + [
            (i.id, i.name)
            for i in security_systems
        ]


class EnterpriseHonourForm((WphForm)):
    """
    add by zhouhl 2018-05-24
    企业荣誉资质表单
    """
    weight = StringField('排序', validators=[Optional()])
    honour_img = StringField('荣誉资质图片', validators=[Optional()])
    honour_title = StringField('荣誉资质描述', validators=[Optional()])


class EnterprisePictureForm(WphForm):
    """
    add by zhouhl 2018-05-24
    企业公司图片表单
    """
    weight = StringField('排序', validators=[Optional()])
    picture_img = StringField('公司图片', validators=[Optional()])
    picture_title = StringField('图片描述', validators=[Optional()])


class EnterpriseMediaReportForm(WphForm):
    weight = StringField('排序', validators=[Optional()])
    report_img = StringField('媒体报道', validators=[Optional()])
    report_title = StringField('报道标题', validators=[Optional()])
    report_text = StringField('报道内容', validators=[Optional()])


class DispatchRuleForm(WphForm):
    """
    add by lina 2018-08-18
    公司派单规则
    """
    color = NonValidatingSelectField(
        '颜色',
        validators=[Optional()],
        choices=NotChoices + ColorRule.to_options(),
        coerce=int
    )
    span = NonValidatingSelectField(
        '间距',
        validators=[Optional()],
        choices=NotChoices + SpanRule.to_options(),
        coerce=int
    )
    size_op = NonValidatingSelectField(
        '面积符号',
        validators=[Optional()],
        choices=NotChoices + SizeOperator.to_options(),
        coerce=int
    )
    size = IntegerField('面积数值', validators=[Optional()])

